<template>
	<view class="itemorder">
		<view class="item" @click="orderDetails(item)">

			<view class="top">
		<!-- 		{{item.img[0]}} -->
				<image :src="item.img[0]" mode=""></image>
				<view class="ri">
					<view class="zz">
						<text>{{item.serviceName[0]}}</text>
						<text style="color: #d4dbd9;">预约时间：<text style="color:#3e4b53;">{{item.time[0]}}</text></text>
					</view>
					<text style="color:#f74346;" v-if="item.fwstatus==0">待上门</text>
					<text style="color:#f74346;" v-else-if="item.fwstatus==1">待完工</text>
					<!-- <text style="color:#adadad;" v-else>已完成</text> -->
					<text style="color:#adadad;" v-else>{{item.orderStatus}}</text>
				</view>
			</view>
			<view class="bot">
				<text>{{item.userAddress.region.label}}-{{item.userAddress.detailed}}</text>
			</view>
		</view>
		<!-- 按钮 -->
		<view class="btn">
			<!-- {{item.orderStatus}} -->
			<view class="bts" v-if="item.orderStatus == '待服务'">

				<view class="bt" @click="goservice(item)">

					<u-button type="primary" size="mini" color="#f74346" shape="circle" text="前往服务地点"></u-button>
				</view>
				<view class="bt" @click="goStart(item)">
					<u-button type="primary" size="mini" color="#f74346" shape="circle" text="开始服务"></u-button>
				</view>
			</view>
			<view class="bts" v-if="item.orderStatus=='服务中'">
				<view class="bt" @click="goEnd(item)">
					<u-button type="primary" size="mini" color="#f74346" shape="circle" text="服务完成"></u-button>
				</view>
			</view>
			<view class="bts" v-else>
				<!-- cxxx -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: ["item"],
		data() {
			return {
				title: 'Hello',
				index: null,
				array: ['高德地图'],
				arrayObj: [{
						has: plus.runtime.isApplicationExist({
							pname: 'com.autonavi.minimap',
							action: 'androidamap://'
						}), //高德
						url: "androidamap://viewMap?sourceApplication=Hello%20uni-app&poiname=九江基地&lat=39.9631018208&lon=116.3406135236&dev=0"
					},
					{
						has: plus.runtime.isApplicationExist({
							pname: 'com.baidu.BaiduMap',
							action: 'baidumap://'
						}), //百度
						url: "baidumap://map/marker?location=39.968789,116.347247&title=DCloud&src=Hello%20uni-app"
					}
				],
				appStore: "market://details?id=com.tencent.mm",
				// url:"https://mobile.amap.com/"
			}
		},
		onShow() {
			// console.log("item", item);
		},
		methods: {
			orderDetails(e) {

				// console.log(e);
				// if (e.fwstatus == 2) {
				uni.navigateTo({
					url: "/pages/order/orderDetails/orderDetails?item=" + JSON.stringify(e)
				});

				// }





			},
			goStart(e) {
				console.log("开始服务", e);
				// uni.navigateTo({
				// 	url: "/pages/order/startService/startService"
				// })
				this.$http.put('/buyrOrder/update', {
					id: e.id,
					orderStatus: "服务中"
				}).then(res => {
					uni.showToast({
						title:"开始服务中……"
					})
					console.log('res', res);
				})
			},
			goEnd(e) {
				// uni.navigateTo({
				// 	url: "/pages/order/endService/endService"
				// })
				console.log("完成", e);
				this.$http.put('/buyrOrder/update', {
					id: e.id,
					orderStatus: "已完成"
				}).then(res => {
					uni.showToast({
						title:"开始已完成"
					})
					console.log('res', res);
				})
			},
			goservice(item) {
				// console.log("suijie", item.userAddress.region.label+""+item.userAddress.detailed);
				let fullAddress = item.userAddress.region.label + "" + item.userAddress.detailed;
				let cleanedAddress = fullAddress.replace(/-/g, '');
				console.log(cleanedAddress);
				this.checkAndNavigate(cleanedAddress)
				// uni.navigateTo({
				// 	url: "/pages/order/goservice/goservice"
				// })
			},
			bindPickerChange(e) {
				this.index = e.detail.value
				let {
					index,
					arrayObj,
					appStore
				} = this
				let has = arrayObj[index].has
				let url = arrayObj[index].url
				if (plus.os.name == "Android") {
					//安卓端
					if (has) {
						plus.runtime.openURL(url)
					} else {
						plus.runtime.openURL(url)
					}
				} else {
					//ios端

				}
			},
			checkAndNavigate(e) {
				var hasAmap = plus.runtime.isApplicationExist({
					pname: 'com.autonavi.minimap',
					action: 'androidamap://'
				});
				console.log("是否安装", hasAmap);
				if (!hasAmap) {
					uni.showModal({
						title: '未安装高德地图',
						content: '检测到您尚未安装高德地图，是否现在下载安装？',
						success: (res) => {
							if (res.confirm) {
								// 用户点击确定，跳转至高德地图官方下载地址
								// this.openDownloadPage();
								plus.runtime.openURL("https://mobile.amap.com/", function(res) {
									console.log(res);
								});
							}
						}
					});
				}
				this.index = 0
				let {
					index,
					arrayObj,
					appStore
				} = this
				let has = arrayObj[index].has
				// let url = arrayObj[index].url
				if (plus.os.name == "Android") {
					//安卓端
					console.log("has", has);
					if (has) {
						plus.runtime.openURL(
							`androidamap://viewMap?sourceApplication=Hello%20uni-app&poiname=${e}&lat=39.9631018208&lon=116.3406135236&dev=0`
						)
					} else {
						plus.runtime.openURL(url)
					}
				}
				//   const amapUrl = 'amap://'; // 高德地图的 URI Scheme

				//   // 尝试通过打开 URI 来检测高德地图是否安装
				//   uni.navigateTo({
				//     url: amapUrl,
				//     success: () => {
				//       // 如果打开成功，说明高德地图已安装
				//       console.log('高德地图已安装');
				//       // 你可以在这里执行跳转到高德地图的操作
				//       // uni.navigateTo({
				//       //   url: '/pages/yourPage/yourPage' // 自定义的页面或操作
				//       // });
				// console.log("地点",e);
				// plus.runtime.openURL(`androidamap://viewMap?sourceApplication=Hello%20uni-app&poiname=${e}&lat=39.9631018208&lon=116.3406135236&dev=0`)
				//     },
				//     fail: () => {
				//       // 如果打开失败，说明高德地图未安装
				//       console.log('高德地图未安装');
				//       // 跳转到高德地图官方下载页面

				//        uni.showModal({
				//                  title: '未安装高德地图',
				//                  content: '检测到您尚未安装高德地图，是否现在下载安装？',
				//                  success: (res) => {
				//                    if (res.confirm) {
				//                      // 用户点击确定，跳转至高德地图官方下载地址
				//                      // this.openDownloadPage();
				// 		 plus.runtime.openURL(this.url, function(res) {
				// 		                   console.log(res);  
				// 		               });
				//                    }
				//                  }
				//                });
				//     }
				//   });
			},

			openDownloadPage() {
				// 提供下载页面的选择
				uni.showActionSheet({
					itemList: ['微信内置浏览器', 'Safari', 'Chrome', '系统默认浏览器'],
					success: (res) => {
						let browserUrl;
						switch (res.tapIndex) {
							case 0:
								// 微信内置浏览器
								browserUrl = downloadUrl;
								break;
							case 1:
								// Safari
								browserUrl = downloadUrl;
								break;
							case 2:
								// Chrome
								browserUrl = downloadUrl;
								break;
							case 3:
								// 系统默认浏览器
								browserUrl = downloadUrl;
								break;
						}
						// 跳转至下载页面
						if (browserUrl) {
							uni.navigateTo({
								url: browserUrl
							});
						}
					},
					fail: () => {
						console.log('用户取消了选择');
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.itemorder {


		.item {


			padding: 8px;


			.top {
				display: flex;
				justify-content: space-between;

				image {
					width: 55px;
					height: 45px;
					border-radius: 6px;
				}

				.ri {
					display: flex;

					.zz {
						display: flex;
						flex-direction: column;
						align-items: flex-start;

						text {
							margin: 2px;
						}
					}
				}

			}

			.bot {
				margin-top: 5px;
			}
		}

		.btn {

			.bts {
				margin: 8px;
				display: flex;
				justify-content: flex-end;
				align-items: center;

				.bt {
					margin-right: 5px;
				}
			}
		}
	}
</style>